class Blocks(object):
    def __init__(self, _blockArray, _boundaryArray, _width, _height) -> None:
        self.blockArray = _blockArray
        self.boundaryArray = _boundaryArray
        self.width = _width # 以管道列第一条管道的尺寸为栅格化标准
        self.height = _height
        self.wall_grids = []

class WallBlocks(Blocks):
    def __init__(self, _blockArray, _boundaryArray, _width, _height, _installation) -> None:
        self.blockArray = _blockArray
        self.boundaryArray = _boundaryArray
        self.startx = self.boundaryArray[0]
        self.starty = self.boundaryArray[1]
        self.startz = self.boundaryArray[2]
        self.width = _width # 以管道列第一条管道的尺寸为栅格化标准
        self.height = _height
        self.installationSpace = _installation # 建筑结构考虑安装空间
        self.wall_grids = []
        self.single_grids = []
        self.Point_to_Grid() # 获取栅格化之后的建筑结构

    def getWallGrid(self):
        return self.wall_grids

    def getSingleGrid(self):
        return self.single_grids

    def getBoundary(self):
        return self.boundaryArray

    def Point_to_Grid(self):
        for pair in self.blockArray:
            start_point = pair[0]
            end_point  = pair[1]
            width = self.isWall(pair[2])
            height = self.isWall(pair[3])
            delta_x = abs(start_point[0] - end_point[0])
            delta_y = abs(start_point[1] - end_point[1])
            delta_z = abs(start_point[2] - end_point[2])
            x1 = x2 = x3 = x4 = y1 = y2 = y3 = y4 = z1 = z2 = 0
            if (delta_x >= delta_y and delta_x >= delta_z): #x为主方向
                x1 = x3 = self.xcord_to_grid(start_point[0], 0, self.width)
                x2 = x4 = self.xcord_to_grid(end_point[0], 0, self.width)
                y1 = y4 = self.ycord_to_grid(start_point[1], -width/2, self.width)
                y2 = y3 = self.ycord_to_grid(start_point[1], width/2, self.width)
                z1 = self.zcord_to_grid(start_point[2], height/-2, self.height)
                z2 = self.zcord_to_grid(height, self.startz, self.height)
            elif (delta_y >= delta_x and delta_y >= delta_z): #y为主方向
                y1 = y4 = self.ycord_to_grid(start_point[1], 0, self.width)
                y2 = y3 = self.ycord_to_grid(end_point[1], 0, self.width)
                x1 = x3 = self.xcord_to_grid(start_point[0], -width/2, self.width)
                x2 = x4 = self.xcord_to_grid(start_point[0], width/2, self.width)
                z1 = self.zcord_to_grid(start_point[2], height/-2, self.height)
                z2 = self.zcord_to_grid(height, self.startz, self.height)
            else:
                y1 = y4 = self.ycord_to_grid(start_point[1], -width/2, self.width)
                y2 = y3 = self.ycord_to_grid(start_point[1], width/2, self.width)
                x1 = x3 = self.xcord_to_grid(start_point[0], -width/2, self.width)
                x2 = x4 = self.xcord_to_grid(start_point[0], width/2, self.width)
                z1 = self.zcord_to_grid(start_point[2], 0, self.height)
                z2 = self.zcord_to_grid(end_point[2], 0, self.height)
                tmp = z1
                z1 = min(z1, z2)
                z2 = max(tmp, z2)
                z2 = z2 - z1     
            if int(x1) == int(x2) and int(x3) == int(x4) and int(y1) == int(y2) and int(y3) == int(y4):
                wall_grid = [x1,y1,z1+z2]
                wall_grid = [int(num) for num in wall_grid]
                self.single_grids.append(wall_grid)
            else:
                wall_grid = [z1,x1,y1,x2,y2,x3,y3,x4,y4,z2]
                wall_grid = [int(num) for num in wall_grid]
                self.wall_grids.append(wall_grid)

    def xcord_to_grid(self, cord, bias, unit):
        return (cord + bias - self.startx) / unit

    def ycord_to_grid(self, cord, bias, unit):
        #print(cord, bias, self.boundaryArray[1], unit)
        return (cord + bias - self.starty) / unit

    def zcord_to_grid(self, cord, bias, unit):
        return (cord + bias - self.startz) / unit
    
    def isWall(self, width_height):
        return width_height + 2 * self.installationSpace # 建筑结构考虑安装空间

class PipeBlocks(Blocks):
    def __init__(self, _blockArray, _boundaryArray, _width, _height, _installation, _insulating) -> None:
        self.blockArray = _blockArray
        self.boundaryArray = _boundaryArray
        self.startx = self.boundaryArray[0]
        self.starty = self.boundaryArray[1]
        self.startz = self.boundaryArray[2]
        self.width = _width # 以管道列第一条管道的尺寸为栅格化标准
        self.height = _height
        self.installationSpace = _installation # 管道障碍物考虑绝缘层
        self.insulatingSpace = _insulating # 管道障碍物考虑绝缘层
        self.wall_grids = []
        self.single_grids = []
        self.Point_to_Grid() # 获取栅格化之后的建筑结构

    def getWallGrid(self):
        return self.wall_grids

    def getSingleGrid(self):
        return self.single_grids

    def getBoundary(self):
        return self.boundaryArray

    def Point_to_Grid(self):
        for pair in self.blockArray:
            start_point = pair[0]
            end_point  = pair[1]
            width = self.isWall(pair[2])
            height = self.isWall(pair[3])
            delta_x = abs(start_point[0] - end_point[0])
            delta_y = abs(start_point[1] - end_point[1])
            delta_z = abs(start_point[2] - end_point[2])
            delta_x = abs(start_point[0] - end_point[0])
            delta_y = abs(start_point[1] - end_point[1])
            delta_z = abs(start_point[2] - end_point[2])
            x1 = x2 = x3 = x4 = y1 = y2 = y3 = y4 = z1 = z2 = 0
            if (delta_x >= delta_y and delta_x >= delta_z): #x为主方向
                x1 = x3 = self.xcord_to_grid(start_point[0], 0, self.width)
                x2 = x4 = self.xcord_to_grid(end_point[0], 0, self.width)
                y1 = y4 = self.ycord_to_grid(start_point[1], -width/2, self.width)
                y2 = y3 = self.ycord_to_grid(start_point[1], width/2, self.width)
                z1 = self.zcord_to_grid(start_point[2], height/-2, self.height)
                z2 = self.zcord_to_grid(height, self.startz, self.height)

            elif (delta_y >= delta_x and delta_y >= delta_z): #y为主方向
                y1 = y4 = self.ycord_to_grid(start_point[1], 0, self.width)
                y2 = y3 = self.ycord_to_grid(end_point[1], 0, self.width)
                x1 = x3 = self.xcord_to_grid(start_point[0], -width/2, self.width)
                x2 = x4 = self.xcord_to_grid(start_point[0], width/2, self.width)
                z1 = self.zcord_to_grid(start_point[2], height/-2, self.height)
                z2 = self.zcord_to_grid(height, self.startz, self.height)

            else:
                y1 = y4 = self.ycord_to_grid(start_point[1], -width/2, self.width)
                y2 = y3 = self.ycord_to_grid(start_point[1], width/2, self.width)
                x1 = x3 = self.xcord_to_grid(start_point[0], -width/2, self.width)
                x2 = x4 = self.xcord_to_grid(start_point[0], width/2, self.width)
                z1 = self.zcord_to_grid(start_point[2], 0, self.height)
                z2 = self.zcord_to_grid(end_point[2], 0, self.height)
                tmp = z1
                z1 = min(z1, z2)
                z2 = max(tmp, z2)
                z2 = z2 - z1     
            if int(x1) == int(x2) and int(x3) == int(x4) and int(y1) == int(y2) and int(y3) == int(y4):
                wall_grid = [x1,y1,z1+z2]
                wall_grid = [int(num) for num in wall_grid]
                self.single_grids.append(wall_grid)
            else:
                wall_grid = [z1,x1,y1,x2,y2,x3,y3,x4,y4,z2]
                wall_grid = [int(num) for num in wall_grid]
                self.wall_grids.append(wall_grid)

    def xcord_to_grid(self, cord, bias, unit):
        return (cord + bias - self.startx) / unit

    def ycord_to_grid(self, cord, bias, unit):
        #print(cord, bias, self.boundaryArray[1], unit)
        return (cord + bias - self.starty) / unit

    def zcord_to_grid(self, cord, bias, unit):
        return (cord + bias - self.startz) / unit

    def isWall(self, width_height):
        return width_height + 2 * self.installationSpace + 2 * self.insulatingSpace # 已有管道考虑安装空间和绝缘层